package org.sgx.gwteditors.testgallery.gallery;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverTableEditor1;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.ui.Panel;

/**
 * PH table 1
 * @author sg
 * 
 */
public class PropertyHaverTableEditor1Test1 implements Test {

	@Override
	public void test(Panel parent) {
		new EditorFramework1().start(); // do not forget of initialize the gwteditors framework !
		PropertyHaverTableEditor1<Contact> ed = new PropertyHaverTableEditor1<Contact>(); 
		ed.load(getAllContacts().toArray(new Contact[]{}));
		parent.add(ed); 
	}
	
	//model
	public static abstract class Contact implements EditableBean {
		String name, address;
		Boolean male; 
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public Boolean getMale() {
			return male;
		}
		public void setMale(Boolean male) {
			this.male = male;
		} 		
	}
	private static List<Contact> getAllContacts() {
		final TestUtil testUtil = TestUtil.getInstance(); 
		List<Contact> l = new LinkedList<PropertyHaverTableEditor1Test1.Contact>(); 
		for (int i = 0; i < 12; i++) {
			Contact c = GWT.create(Contact.class);
			c.setName(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
			c.setAddress(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
			//order properties
			c.getProperties().reorder(new String[]{"name", "address", "male"}); 
			l.add(c); 
		}
		return l; 
	}
	
	// test stuff
	@Override
	public String getName() {
		return "PH table 1";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.PropertyHaverTableEditor1Test1());
		
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "PH table 1";
	}
}
